package com.fram.controller.admin;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.sys.PageBean;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProException;
import com.fram.common.exception.ProValiDataException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.entity.MessageData;
import com.fram.entity.User;
import com.fram.service.admin.MessageDataService;

@RestController
@RequestMapping("/admin/{version}/auth/message")
public class MessageDataController {

	private static Logger LOG = LoggerFactory.getLogger(MessageDataController.class);

	@Autowired
	private MessageDataService messageDataService;

	/****/
	@ApiVersion(1.0)
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	private ResponseMsg list(@RequestBody PageBean<MessageData> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(messageDataService.list(request));
		} catch (ProException e) {
			LOG.error("-消息列表->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-消息列表->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/****/
	@ApiVersion(1.0)
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	private ResponseMsg add(@RequestBody List<MessageData> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			messageDataService.send(request);
		} catch (ProException e) {
			LOG.error("-发布系统信息->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-发布系统信息->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}

	/** 下拉 **/
	@ApiVersion(1.0)
	@RequestMapping(value = "/dropdown", method = RequestMethod.POST)
	private ResponseMsg dropDown(@RequestBody User request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			if (StringUtils.isEmpty(request.getNickName())) {
				throw new ProValiDataException(ErrCode.ERR_PARAMETER.getReturnMsg());
			}
			msg.setData(messageDataService.dropDown(request));
		} catch (ProException e) {
			LOG.error("-消息管理 用户下拉->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-消息管理 用户下拉->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
